<?php
/**
 * 优惠券管理
 */
defined('IN_KG') or exit('Access Invalid!');
class store_redpacketControl extends BaseSellerControl{

    public function indexOp(){
        $model_redpacket = Model('redpacket');
        $condition = array();
        $condition['rpacket_t_end_date'] = array('gt', time());
        $condition['rpacket_t_start_date'] = array('lt', time());
        $rpt_list = $model_redpacket->getRptTemplateList($condition);

        Tpl::output('rpt_list',$rpt_list);
        Tpl::showpage('store_redpacket_template.index') ;
    }

    public function redpacket_sendOp(){
        $param = $_POST;
        print_r($_POST);
        $pwd_code = $param["pwd_code"];
        $member_id = $param["member_id"];
        if (!$pwd_code || !$member_id) {
            showDialog('参数错误');
        }
        $member_info = Model('member')->getMemberInfoByID($member_id);

        //查询优惠券
        $model_redpacket = Model('redpacket');
        $redpacket_info = $model_redpacket->getRedpacketInfo(array('rpacket_pwd'=>md5($pwd_code)));
        if(!$redpacket_info){
            showDialog('优惠券卡密错误');
        }
        if($redpacket_info['rpacket_owner_id'] > 0){
            showDialog('该优惠券卡密已被使用');
        }
        $where = array();
        $where['rpacket_id'] = $redpacket_info['rpacket_id'];
        $update_arr = array();
        $update_arr['rpacket_owner_id'] = $member_info['member_id'];
        $update_arr['rpacket_owner_name'] = $member_info['member_name'];
        $update_arr['rpacket_active_date'] = time();
        $result = $model_redpacket->editRedpacket($where, $update_arr, $member_info['member_id']);
        if($result){
            //更新优惠券模板
            $update_arr = array();
            $update_arr['rpacket_t_giveout'] = array('exp','rpacket_t_giveout+1');
            $model_redpacket->editRptTemplate(array('rpacket_t_id'=>$redpacket_info['rpacket_t_id']),$update_arr);
            showDialog("优惠券领取成功，成功将优惠券发送至用户【$member[member_name]】账号中。",'', 'succ');
        } else {
            showDialog('优惠券领取失败');
        }
    }
}
